package com.example.demo.controller.system;

import com.example.demo.annoation.Log;
import com.example.demo.bean.AjaxRes;
import com.example.demo.bean.sysdict.SysDictDataDO;
import com.example.demo.controller.BaseAdminController;
import com.example.demo.enums.BusinessType;
import com.example.demo.service.ISysDictDataService;
import com.example.demo.dto.sysdict.SysDictDataPageReqVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping(value = "/system/dict_data")
@RestController
public class SysDictDataController extends BaseAdminController {

    @Resource
    private ISysDictDataService sysDictDataService;

    @GetMapping("/list")
    public AjaxRes list(SysDictDataPageReqVO sysDictDataPageReqVO) {
        return AjaxRes.success(sysDictDataService.list(sysDictDataPageReqVO));
    }

    @PostMapping("/insert")
    @Log(title = "新增字典值", businessType = BusinessType.INSERT)
    public AjaxRes insert(@RequestBody SysDictDataDO sysDictDataDO) {
        return toAjax(sysDictDataService.addSysDictData(sysDictDataDO));
    }

    @DeleteMapping("/delete/{dictDataId}")
    @Log(title = "删除字典值", businessType = BusinessType.DELETE)
    public AjaxRes delete(@PathVariable("dictDataId") Long dictDataId) {
        return toAjax(sysDictDataService.deleteSysDictData(new SysDictDataDO().setDictDataId(dictDataId)));
    }

    @PutMapping("/update")
    @Log(title = "修改字典值", businessType = BusinessType.UPDATE)
    public AjaxRes update(@RequestBody SysDictDataDO sysDictDataDO) {
        return toAjax(sysDictDataService.updateSysDictData(sysDictDataDO));
    }

    @GetMapping("/{dictDataId}")
    public AjaxRes query(@PathVariable("dictDataId") Long dictDataId) {
        return AjaxRes.success(sysDictDataService.getSysDictData(new SysDictDataDO().setDictDataId(dictDataId)));
    }

    @GetMapping("/type/{dictType}")
    public AjaxRes queryByType(@PathVariable("dictType") String dictType) {
        return AjaxRes.success(sysDictDataService.listByType(dictType));
    }
}
